<template>
  <div class="set">
    <Row type="flex"
         align="middle"
         justify="center"
         class="set_ct">
      <Col>
      <Form ref="formDynamic" v-show='loaded'
            :model="formDynamic"
            :rules="ruleValidate"
            :label-width="120">
            <FormItem label="配置开关：">
          <i-switch  size="large" v-model='switchStatus'>
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>
        </FormItem>
        <FormItem prop='radio' v-show='switchStatus'
                  label="是否打印小票：">
          <RadioGroup v-model="formDynamic.radio">
            <Radio label="1">是</Radio>
            <Radio label="0"
                   style="margin-left:85px">否</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  @click="handleSubmit('formDynamic')"
                  class="button">确定</Button>
          <Button @click="cancel"
                  class="button btn1">取消</Button>
        </FormItem>
      </Form>
      </Col>
    </Row>
  </div>
</template>
<script>
import './index.less'
// import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      loaded: false,
      switchStatus: true,
      formDynamic: {
        radio: ''
      },
      ruleValidate: {
        radio: [{ required: true, message: '请选择是否打印小票', trigger: 'blur' }]
      },
      paramter: {
        'shopId': this.$route.query.id,
        'merchantId': this.$store.state.user.merchantId,
        'msType': '1'
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, { 'enablePrintReceipt': this.formDynamic.radio, 'enablePrintReceiptSwitch': this.switchStatus ? '1' : '0' })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      // this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'store_setting', params: { id: this.$route.query.id }
      //   })
      // })
      if (this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 1].name !== 'store_setting') {
        this.$nextTick(() => {
          this.$router.push({
            name: 'store_setting', params: { id: this.$route.query.id }
          })
        })
      }
    },
    cancel () {
      // this.$refs.formDynamic.resetFields()
      this.closeModal()
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'enablePrintReceipt,enablePrintReceiptSwitch' })
    getDicQuery(obj).then((res) => {
      this.loaded = true
      if (res.data.status === 200) {
        this.formDynamic.radio = res.data.data[0].enablePrintReceipt
        if (res.data.data[1].enablePrintReceiptSwitch === '0' || res.data.data[1].enablePrintReceiptSwitch === 0) {
          this.switchStatus = false
        } else {
          this.switchStatus = true
        }
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
    if (!this.hasPermission('moms:setting:consumerprint')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
